package com.ruoyi.project.xcx.myhome.category.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.project.xcx.myhome.goods.domain.XcxMyhomeGoods;
import com.ruoyi.project.xcx.myhome.goods.mapper.XcxMyhomeGoodsMapper;
import com.ruoyi.project.xcx.user.domain.XcxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.project.xcx.myhome.category.mapper.XcxMyhomeCategoryMapper;
import com.ruoyi.project.xcx.myhome.category.domain.XcxMyhomeCategory;
import com.ruoyi.project.xcx.myhome.category.service.IXcxMyhomeCategoryService;
import com.ruoyi.common.utils.text.Convert;

/**
 * myhome分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-03-10
 */
@Service
public class XcxMyhomeCategoryServiceImpl implements IXcxMyhomeCategoryService 
{
    @Autowired
    private XcxMyhomeCategoryMapper xcxMyhomeCategoryMapper;
    @Autowired
    private XcxMyhomeGoodsMapper xcxMyhomeGoodsMapper;

    @Override
    public void deleteCategory(Long categoryId, XcxUser user) {
        // 查询当前分类下是否有物品
        XcxMyhomeGoods queryGoods = new XcxMyhomeGoods();
        queryGoods.setCategoryId(categoryId);
        queryGoods.setUserId(user.getId());
        List<XcxMyhomeGoods> goodsList = xcxMyhomeGoodsMapper.selectGoodsByName(queryGoods);
        if(goodsList.size()>0){
            throw new BusinessException("分类下存在物品不能删除");
        }
        xcxMyhomeCategoryMapper.deleteByIdAndUserId(categoryId, user.getId());
    }

    @Override
    public Map<Long, String> queryAllCategoryName(Long userId) {
        XcxMyhomeCategory queryCategory = new XcxMyhomeCategory();
        queryCategory.setUserId(userId);
        List<XcxMyhomeCategory> categoryList = xcxMyhomeCategoryMapper.selectXcxMyhomeCategoryList(queryCategory);
        return categoryList.stream().collect(Collectors.toMap(XcxMyhomeCategory::getId, XcxMyhomeCategory::getName));
    }

    @Override
    public List<XcxMyhomeCategory> listCategory(XcxUser user) {
        XcxMyhomeCategory queryCategory = new XcxMyhomeCategory();
        queryCategory.setUserId(user.getId());
        List<XcxMyhomeCategory> categoryList = xcxMyhomeCategoryMapper.selectXcxMyhomeCategoryList(queryCategory);
        categoryList.sort(new Comparator<XcxMyhomeCategory>() {
            @Override
            public int compare(XcxMyhomeCategory o1, XcxMyhomeCategory o2) {
                return o1.getCount().compareTo(o2.getCount());
            }
        });
        return categoryList;
    }

    /**
     * 查询myhome分类
     * 
     * @param id myhome分类ID
     * @return myhome分类
     */
    @Override
    public XcxMyhomeCategory selectXcxMyhomeCategoryById(Long id)
    {
        return xcxMyhomeCategoryMapper.selectXcxMyhomeCategoryById(id);
    }

    /**
     * 查询myhome分类列表
     * 
     * @param xcxMyhomeCategory myhome分类
     * @return myhome分类
     */
    @Override
    public List<XcxMyhomeCategory> selectXcxMyhomeCategoryList(XcxMyhomeCategory xcxMyhomeCategory)
    {
        return xcxMyhomeCategoryMapper.selectXcxMyhomeCategoryList(xcxMyhomeCategory);
    }

    /**
     * 新增myhome分类
     * 
     * @param xcxMyhomeCategory myhome分类
     * @return 结果
     */
    @Override
    public int insertXcxMyhomeCategory(XcxMyhomeCategory xcxMyhomeCategory)
    {
        return xcxMyhomeCategoryMapper.insertXcxMyhomeCategory(xcxMyhomeCategory);
    }

    /**
     * 修改myhome分类
     * 
     * @param xcxMyhomeCategory myhome分类
     * @return 结果
     */
    @Override
    public int updateXcxMyhomeCategory(XcxMyhomeCategory xcxMyhomeCategory)
    {
        return xcxMyhomeCategoryMapper.updateXcxMyhomeCategory(xcxMyhomeCategory);
    }

    /**
     * 删除myhome分类对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteXcxMyhomeCategoryByIds(String ids)
    {
        return xcxMyhomeCategoryMapper.deleteXcxMyhomeCategoryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除myhome分类信息
     * 
     * @param id myhome分类ID
     * @return 结果
     */
    @Override
    public int deleteXcxMyhomeCategoryById(Long id)
    {
        return xcxMyhomeCategoryMapper.deleteXcxMyhomeCategoryById(id);
    }
}
